TypeScript の as const
constアサーション「as const」 (const assertion) | TypeScript入門『サバイバルTypeScript』
変数宣言のときに、末尾にas constをつけるとその値をreadonlyにした上で、リテラル型にしてくれます。
code:ts
const str1 = "hello";
const str2 = "hello" as const; // これは as const がなくても同じ
const array1 = 1, 2, 3;
const array2 = 1, 2, 3 as const;
const obj1 = {
name: "pikachu",
no: 25,
genre: "mouse pokémon",
height: 0.4,
weight: 6.0,
};
const obj2 = {
name: "pikachu",
no: 25,
genre: "mouse pokémon",
height: 0.4,
weight: 6.0,
} as const;